package tech.horgee.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.horgee.common.MyResult;
import tech.horgee.entity.Expense;
import tech.horgee.group.InsertGroup;
import tech.horgee.group.UpdateGroup;
import tech.horgee.service.ExpenseService;

import java.util.Set;

/**
 * 报销表接口
 *
 * @author haojun
 * @since 2023-12-13
 */
@Slf4j
@RestController
@RequestMapping("/expense")
@RequiredArgsConstructor
public class ExpenseController {
    private final ExpenseService service;

    /**
     * 分页查询列表
     *
     * @param index  当前页|1
     * @param size   页大小|10
     * @param entity 查询实体
     * @return 所有数据
     */
//    @SaCheckPermission("expense:query")
    @PostMapping("page/{index}/{size}")
    public MyResult selectAll(@RequestBody(required = false) Expense entity, @PathVariable Long index, @PathVariable Long size) {
        Page<Expense> page = new Page<>(index, size);
        return MyResult.of(service.page(page, entity));
    }

    /**
     * 不分页查询列表
     *
     * @param entity 实体对象
     * @return 查询结果
     */
//    @SaCheckPermission("expense:query")
    @PostMapping("list")
    public MyResult list(@RequestBody(required = false) Expense entity) {
        return MyResult.of(service.list(entity));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
//    @SaCheckPermission("expense:query")
    @GetMapping("{id}")
    public MyResult getById(@PathVariable Long id) {
        return MyResult.of(service.getById(id));
    }

    /**
     * 新增单条数据
     *
     * @param entity 实体对象
     * @return 新增结果
     */
//    @SaCheckPermission("expense:add")
    @PostMapping
    public MyResult save(@RequestBody @Validated(InsertGroup.class) Expense entity) {
        service.save(entity);
        return MyResult.success();
    }

    /**
     * 修改单条数据
     *
     * @param entity 实体对象
     * @return 修改结果
     */
//    @SaCheckPermission("expense:edit")
    @PutMapping
    public MyResult update(@RequestBody @Validated(UpdateGroup.class) Expense entity) {
        service.update(entity);
        return MyResult.success();
    }

    /**
     * 批量删除数据
     *
     * @param ids 主键集合
     * @return 操作结果
     */
//    @SaCheckPermission("expense:delete")
    @PostMapping("delete")
    public MyResult deleteByIds(@RequestBody Set<Long> ids) {
        service.deleteByIds(ids);
        return MyResult.success();
    }

}
